package com.manager.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.manager.pojo.TbCommodity;
import com.manager.pojo.TbInlibrary;
import com.manager.pojo.TbOutlibrary;
import com.manager.service.InLibraryService;
import com.manager.service.OutLibrarService;
import com.manager.service.StorageService;
import com.manager.vo.TbUserEx;

/**
 * 
 * @Title:  PageControl.java     
 * @Description:    页面访问controller
 * @author:      
 * @date:   2019年2月13日 上午10:48:38   
 * @version V1.0
 */
@Controller
public class PageControl {

	@Autowired
	private StorageService storageService;
	
	@Autowired
	private InLibraryService inLibraryService;
	
	@Autowired
	private OutLibrarService outLibrarService;
	
	//根目录转首页访问
	@RequestMapping("/")
	public String root() {
		
		return "index";
	}
	
	//首页访问
	@RequestMapping("/index")
	public String index() {
		
		System.out.println("indexx");
		
		return "index";
	}
	
	//添加用户页面访问
	@RequestMapping("/user-add")
	public String userAdd() {
		
		return "user-add";
	}
	
	//统计数据框架页访问
	@RequestMapping("/echarts4")
	public String echarts4() {
		
		return "echarts4";
	}
	
	//柱形统计页访问
	@RequestMapping("/echars2")
	public String echarts2(String start,String end,Model model) {
	
		TbUserEx tbUserEx = new TbUserEx();
		TbUserEx temptbUserEx = new TbUserEx();
		
		if(start == null || start.isEmpty()) {
			start = "2019-02-20";
		}
		if( end == null || end.isEmpty()) {
			end = "2019-03-10";
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			Date dstart = sdf.parse(start);
			Date dend = sdf.parse(end);
			
			temptbUserEx = storageService.getStorageLists(dstart,dend);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		Long storage = 0L;
		
		List<TbCommodity> storagelist  = (List<TbCommodity>)temptbUserEx.getRows();
		
		for(TbCommodity tbc:storagelist) {
			storage += tbc.getsStorage();
		}
		//==================================
		tbUserEx.setStorage(storage);
		//==================================
		
		try {
			Date dstart = sdf.parse(start);
			Date dend = sdf.parse(end);
			temptbUserEx = inLibraryService.getInLibraryList(dstart,dend);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		storage = 0L;
		
		List<TbInlibrary> inlibrarylist  = (List<TbInlibrary>)temptbUserEx.getRows();
		
		for(TbInlibrary inl:inlibrarylist) {
			storage += inl.getInlibrary();
		}
		
		//==================================
			tbUserEx.setInlibrary(storage);
		//==================================

			
		try {
			Date dstart = sdf.parse(start);
			Date dend = sdf.parse(end);
			temptbUserEx = outLibrarService.getOutLibraryList(dstart,dend);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		storage = 0L;

		List<TbOutlibrary> outliblist = (List<TbOutlibrary>)temptbUserEx.getRows();
		
		for(TbOutlibrary out:outliblist) {
			storage += out.getOutlibrary();
		}
		//==================================
		tbUserEx.setOutlibrary(storage);
		//==================================
		
		model.addAttribute("tbUserEx", tbUserEx);
		return "echars2";
	}
	
	//饼状图统计页访问
	@RequestMapping("/echars3")
	public String echarts3(String start,String end,Model model) {
		
		TbUserEx tbUserEx = new TbUserEx();
		TbUserEx temptbUserEx = new TbUserEx();
		
		if(start == null || start.isEmpty()) {
			start = "2019-02-20";
		}
		if( end == null || end.isEmpty()) {
			end = "2019-03-10";
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		try {
			Date dstart = sdf.parse(start);
			Date dend = sdf.parse(end);
			
			temptbUserEx = storageService.getStorageLists(dstart,dend);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		BigDecimal dec = new BigDecimal(0);
		
		List<TbCommodity> storagelist  = (List<TbCommodity>)temptbUserEx.getRows();
		
		for(TbCommodity tbc:storagelist) {
			
			dec = dec.add(new BigDecimal(tbc.getsStorage()).multiply(tbc.getPrice()));
		}
		//==================================
		tbUserEx.setBigstorage(dec);
		//==================================
		
		try {
			Date dstart = sdf.parse(start);
			Date dend = sdf.parse(end);
			temptbUserEx = inLibraryService.getInLibraryList(dstart,dend);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		dec = new BigDecimal(0);
		
		List<TbInlibrary> inlibrarylist  = (List<TbInlibrary>)temptbUserEx.getRows();
		
		for(TbInlibrary inl:inlibrarylist) {
			dec = dec.add(new BigDecimal(inl.getInlibrary()).multiply(inl.getPrice()));
		}
		
		//==================================
			tbUserEx.setBiginlibrary(dec);
		//==================================

			
		try {
			Date dstart = sdf.parse(start);
			Date dend = sdf.parse(end);
			temptbUserEx = outLibrarService.getOutLibraryList(dstart,dend);
		}catch(Exception e) {
			e.printStackTrace();
		}
		
		dec = new BigDecimal(0);

		List<TbOutlibrary> outliblist = (List<TbOutlibrary>)temptbUserEx.getRows();
		
		for(TbOutlibrary out:outliblist) {
			dec = dec.add(new BigDecimal(out.getOutlibrary()).multiply(out.getPrice()));
		}
		//==================================
		tbUserEx.setBigoutlibrary(dec);
		//==================================
		
		model.addAttribute("tbUserEx", tbUserEx);
		
		return "echars3";
	}
	
	
	//欢迎页访问
	@RequestMapping("/welcome")
	public String welcome() {
		
		return "welcome";
	}
}
